package com.passengerflowmanagementsystem.web.controller.em;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.passengerflowmanagementsystem.statistics.domain.*;
import com.passengerflowmanagementsystem.statistics.domain.hk.HkResult;
import com.passengerflowmanagementsystem.statistics.domain.vo.RegionVo;
import com.passengerflowmanagementsystem.statistics.service.IEmExhibitionShopService;
import com.passengerflowmanagementsystem.statistics.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.passengerflowmanagementsystem.common.annotation.Log;
import com.passengerflowmanagementsystem.common.core.controller.BaseController;
import com.passengerflowmanagementsystem.common.core.domain.AjaxResult;
import com.passengerflowmanagementsystem.common.enums.BusinessType;
import com.passengerflowmanagementsystem.statistics.service.IEmExhibitionHallService;
import com.passengerflowmanagementsystem.common.utils.poi.ExcelUtil;
import com.passengerflowmanagementsystem.common.core.page.TableDataInfo;

/**
 * 展厅Controller
 * 
 * @author sheng
 * @date 2024-10-28
 */
@RestController
@RequestMapping("/em/hall")
public class EmExhibitionHallController extends BaseController
{
    @Autowired
    private IEmExhibitionHallService emExhibitionHallService;

    @Autowired
    private IEmExhibitionShopService emExhibitionShopService;

    @Autowired
    private IRegionService regionService;

    /**
     * 查询展厅列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EmExhibitionHall emExhibitionHall)
    {
        startPage();
        List<EmExhibitionHall> list = emExhibitionHallService.selectEmExhibitionHallList(emExhibitionHall);
        return getDataTable(list);
    }

    /**
     * 导出展厅列表
     */
    @Log(title = "【导出展厅列表】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmExhibitionHall emExhibitionHall)
    {
        List<EmExhibitionHall> list = emExhibitionHallService.selectEmExhibitionHallList(emExhibitionHall);
        ExcelUtil<EmExhibitionHall> util = new ExcelUtil<EmExhibitionHall>(EmExhibitionHall.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取展厅详细信息
     */
    @GetMapping(value = "/{hallId}")
    public AjaxResult getInfo(@PathVariable("hallId") Long hallId)
    {
        return success(emExhibitionHallService.selectEmExhibitionHallByHallId(hallId));
    }

    /**
     * 新增展厅
     */
    @Log(title = "【新增展厅】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmExhibitionHall emExhibitionHall)
    {
        //新增展厅
        emExhibitionHallService.insertEmExhibitionHall(emExhibitionHall);
        EmExhibitionShop shop = emExhibitionShopService.selectEmExhibitionShopByShopId(emExhibitionHall.getShopId());
        //新增海康区域
        RegionVo regionVo = new RegionVo();
        regionVo.setClientId(emExhibitionHall.getHallId());
        regionVo.setParentIndexCode(shop.getHkRegionIndexCode());
        regionVo.setRegionName(emExhibitionHall.getHallName());
        regionVo.setRegionType("10");
        regionVo.setDescription(emExhibitionHall.getRemark());
        RegionVo[] regionVos = new RegionVo[]{regionVo};
        String str = regionService.regionBatchAdd(regionVos);
        HkResult hkResult = JSONObject.parseObject(str, HkResult.class);
        //获取海康区域id并关联到展厅
        emExhibitionHall.setHkRegionIndexCode(hkResult.getData().getSuccesses()[0].getRegionIndexCode());
        return toAjax(emExhibitionHallService.updateEmExhibitionHall(emExhibitionHall));
    }

    /**
     * 修改展厅
     */
    @Log(title = "【修改展厅】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmExhibitionHall emExhibitionHall)
    {
        RegionVo regionVo = new RegionVo();
        regionVo.setRegionIndexCode(emExhibitionHall.getHkRegionIndexCode());
        regionVo.setRegionName(emExhibitionHall.getHallName());
        regionService.regionSingleUpdate(regionVo);

        return toAjax(emExhibitionHallService.updateEmExhibitionHall(emExhibitionHall));
    }

    /**
     * 删除展厅
     */
    @Log(title = "【删除展厅】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{hallIds}")
    public AjaxResult remove(@PathVariable Long[] hallIds)
    {
        //定义arry把Long[]展厅Id添加进去，然后转为String[]类型再删除
        ArrayList<String> array = new ArrayList<>();
        for (int i = 0; i < hallIds.length; i++) {
            EmExhibitionHall emExhibitionHall = emExhibitionHallService.selectEmExhibitionHallByHallId(hallIds[i]);
            array.add(emExhibitionHall.getHkRegionIndexCode());
        }
        String[] indexCodes = new String[array.size()];
        array.toArray(indexCodes);
        regionService.regionBatchDelete(indexCodes);
        return toAjax(emExhibitionHallService.deleteEmExhibitionHallByHallIds(hallIds));
    }
}
